package com.zt.ztrob.api;

import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.blade.kit.http.HttpRequest;
import com.zt.ztrob.utils.Constant;
import com.zt.ztrob.utils.GetGroupNickName;

public class MsgAPI {

    /**
     * 获取回复
     * @param content 发送消息内容
     * @param userId  用户标识
     * @param location 位置
     * @return
     */
    public static String getReply(String content, String userId, String location, String fromUserName, String groupName, String groupInf,
            String uin) {
        String reply = null;
        JSONObject retJson = null;
        String ret = null;
        HttpRequest req = null;
        String str = null;
        new GetGroupNickName();
        if (content != null && content != "") {
            String c = content.substring(5);
            try {
                str = GetGroupNickName.getURLEncoderString(c);
                groupName = GetGroupNickName.getURLEncoderString(groupName);

                if (content.contains("更新")) {
                    req = HttpRequest.get(
                            Constant.ROBURL + "robBindOper.do?method=addRobBind&wid=" + fromUserName + "&groupName=" + groupName + "&uin=" + uin);
                    System.out.println(req);

                } else if (content.contains("绑定")) {
                    String agentNo = c.replace("绑定", "");
                    req = HttpRequest.get(Constant.ROBURL + "agentBind.do?method=addAgent&agentNo=" + agentNo + "&groupInf=" + groupInf);
                    System.out.println(req);
                } else if (content.contains("开房")) {
                    req = HttpRequest.get(Constant.ROBURL + "roomManage.do?method=createRoom&groupId=" + fromUserName + "&urlKey=" + str);
                    System.out.println(req);
                } else {
                    for (int i = 0; i < Constant.KEYWORD.size(); i++) {
                        if (content.contains(Constant.KEYWORD.get(i))) {
                            req = HttpRequest
                                    .get(Constant.ROBURL + "interfaceManage.do?method=queryInterface&groupId=" + fromUserName + "&urlKey=" + str);
                            System.out.println(req);
                        }
                    }

                }
                ret = req.body();
                System.out.println(ret);
                req.disconnect();
                System.out.println("等待两秒在发送");
                Thread.sleep(2000);
            } catch (Exception e) {
                for (int i = 0; i < Constant.KEYWORD.size(); i++) {
                    if (content.contains(Constant.KEYWORD.get(i))) {
                        return "连接服务器失败";
                    }
                }
                return null;
            }
            retJson = JSONObject.parseObject(ret);
            reply = handelReply(retJson, content);

        }
        return reply;

    }

    public static String handelReply(JSONObject retJson, String content) {
        String replyMsg = (String) retJson.get("errorMsg");
        String replyCode = (String) retJson.get("errorCode");

        if (!replyMsg.equals("PLT999999")) {
            if (content.contains("更新")) {
                if (replyCode.equals("0000")) {
                    return replyMsg;
                }
                return "更新失败";
            } else if (content.contains("绑定")) {
                if (replyCode.equals("0000")) {
                    String ranCode = (String) retJson.get("ranCode");
                    if (ranCode.trim() != null && !ranCode.trim().equals("")) {
                        String code = "验证码：\n" + ranCode;
                        String testUrl = "https//testRegister.com";
                        return "绑定代理成功！" + code + "\n" + testUrl;
                    }
                    return "该群已绑定代理，无需重复绑定";
                }
                return "绑定失败";
            } else if (content.contains("开房")) {
                if (replyCode.equals("0000")) {
                    String roomMsg = (String) retJson.get("roomNo");
                    return roomMsg;
                } else {
                    return replyMsg;
                }
            } else {
                if (replyCode.equals("0000")) {

                    String reply = getList(retJson.toString());
                    System.out.println(reply);
                    reply = getList(reply);
                    return reply;
                }
            }
        } else {
            return null;
        }
        return null;
    }

    public static String getList(String a) {
        try {
            LinkedHashMap<String, String> jsonMap = JSON.parseObject(a, new TypeReference<LinkedHashMap<String, String>>() {
            });
            for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
                // System.out.println(entry.getKey() + ":" + entry.getValue());
                if (entry.getKey().contains("List")) {
                    System.out.println(1);
                    return entry.getValue();
                } else if (!(entry.getKey().contains("errorCode")) && !(entry.getKey().contains("errorMsg"))) {
                    if ((entry.getKey().contains("listNum"))) {

                    } else if ((entry.getKey().contains("totalPage"))) {

                    } else {
                        return entry.getValue();
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;

    }

    /*public static void main(String[] args) {
    	new MsgAPI().getReply("11111激活3380", "userId", "location", "@@c1b540fd2fc780a46961369415ab33190b47c2cab532dfdbc4c9562d70ed0321","xiaoqingxin","{\"groupName\":\"rob\",\"groupCount\":9,\"GroupOwner\":\"凌云知晓\"}","");
    
    }*/
}
